import axios from 'axios' // 引入axios
import { request } from 'http';
import store from '../src/vuex/store' // 引入Vuex
// import router from '../router' // 引入vue-router
// import { Message } from 'element-ui' //局部引入UI框架组件
// import Qs from 'qs'
export const domain = 'http://crmapi.lyqswl.com';
export const corpid = 'wwb1448bd79e4dfde9'; // 企业corpid
export const agentId = '1000007'; // 企业id
export const corpsecret = 'S-3ac6fwcZczzfF63PHBvjpkuXMfTY5fCGUYiDTyvK4'; // 应用的凭证密钥

// 环境的切换
if (process.env.NODE_ENV === 'development') { // 开发环境域名
  // axios.defaults.baseURL = 'http://www.crm.com/index.php';
  axios.defaults.baseURL = 'http://crmapi.lyqswl.com/index.php' // 生产环境
} else {
  axios.defaults.baseURL = 'http://crmapi.lyqswl.com/index.php' // 打包环境
}
// 请求超时时间
axios.defaults.timeout = 20000;

// 请求拦截器
axios.interceptors.request.use(
  config => {
    if (config.method == "post") {
      if (localStorage.getItem("token") && config.url.indexOf('Login/redirect_url') == -1 && config.url.indexOf('Login/check_login') == -1) {
        // config.headers.Token = localStorage.getItem("token")
        config.headers.Token = "123"
      }
    }
    return config;
  },
  error => {
    return Promise.error(error)
  }
)

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 * @param {Object} config [请求时url后直接加的参数，默认为空]
 */
export function get(url, params, config = { add: '' }) {
  return new Promise((resolve, reject) => {
    axios.get(url, {
      params: params
    }, config).then(res => {
      resolve(res.data)
    }).catch(err => {
      reject(err.data)
    })
  })
}
/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 * @param {Object} config [是否启用multipart/form-data格式请求，默认为false]
 */
export function post(url, params, config = { isUpload: false }) {
  return new Promise((resolve, reject) => {
    var keystr = localStorage.getItem("keystr");
    var param = Object(params);
    let keys = "keystr";
    let value = keystr;
    param[keys] = value;
    axios.post(url, param(), config)
      .then(res => {
        resolve(res.data)
      })
      .catch(err => {
        reject(err.data)
      })
  })
}
